import 'dart:convert';
import 'dart:typed_data';

import 'package:flutter/cupertino.dart';
import 'package:flutter_admin/models/odoo/sale_inquiry_form.dart';
// import 'package:flutter_admin/utils/helper/save_file_web.dart';
import 'package:flutter_admin/utils/helper/save_file_mobile.dart'
    if (dart.library.html) 'package:flutter_admin/utils/helper/save_file_web.dart';
import 'package:syncfusion_flutter_xlsio/xlsio.dart';

class SaleInquiryExcel {
  static void generateExcel(
      {String? fileName, SaleInquiryForm? dataForm, List? datatable}) async {
    // Create a new Excel Document.
    final Workbook workbook = Workbook();

    // Accessing sheet via index.
    final Worksheet sheet = workbook.worksheets[0];
    sheet.enableSheetCalculations();
    sheet.getRangeByName('A1:G2').merge();
    for (var i = 0; i < 6; i++) {
      sheet.getRangeByIndex(1, i + 1).rowHeight = 20;
      sheet.getRangeByIndex(2, i + 1).rowHeight = 20;
    }
    sheet.importList(['$fileName（业务员填写）'], 1, 1, false);

    List three = [
      '报价单位：',
      dataForm!.companyName,
      '业务员：',
      dataForm.userName,
      '',
      '事业部：',
      dataForm.deptName
    ];
    sheet.getRangeByName('D3:E3').merge();
    for (var i = 0; i < 6; i++) {
      sheet.getRangeByIndex(3, i + 1).rowHeight = 20;
    }
    sheet.importList(three, 3, 1, false);

    List four = ['', '', '电话：', dataForm.phone, '', '邮箱：', dataForm.email];
    sheet.getRangeByName('D4:E4').merge();
    for (var i = 0; i < 6; i++) {
      sheet.getRangeByIndex(4, i + 1).rowHeight = 20;
    }
    sheet.importList(four, 4, 1, false);

    List five = [
      '询价单位：',
      dataForm.partnerName,
      '项目名称：',
      dataForm.projectName,
      '',
      '客户性质：',
      dataForm.customerNature
    ];
    sheet.getRangeByName('D5:E5').merge();
    for (var i = 0; i < 6; i++) {
      sheet.getRangeByIndex(5, i + 1).rowHeight = 20;
    }
    sheet.importList(five, 5, 1, false);

    List six = [
      '付款方式：',
      dataForm.paymentMethod,
      '到货地点：',
      dataForm.arrivalPlace,
      '',
      '是否有目标价：',
      dataForm.isTargetprice == '1' ? '是' : '否'
    ];
    sheet.getRangeByName('D6:E6').merge();
    for (var i = 0; i < 6; i++) {
      sheet.getRangeByIndex(6, i + 1).rowHeight = 20;
    }
    sheet.importList(six, 6, 1, false);

    sheet.getRangeByName('A7:G7').merge();
    for (var i = 0; i < 6; i++) {
      sheet.getRangeByIndex(7, i + 1).rowHeight = 20;
    }
    sheet.importList([], 7, 1, false);

    List<Map> configure = [
      {
        'name': 'firstNum',
        'label': '序号',
        'columnWidth': 13.0,
      },
      {
        'name': 'productName',
        'label': '产品名称',
        'columnWidth': 34.0,
      },
      {
        'name': 'description',
        'label': '规格型号',
        'columnWidth': 22.0,
      },
      {
        'name': 'technicalRequirement',
        'label': '技术要求',
        'columnWidth': 10.0,
      },
      {
        'name': 'unitPrice',
        'label': '单价',
        'columnWidth': 10.0,
      },
      {
        'name': 'quantity',
        'label': '数量',
        'columnWidth': 19.0,
      },
      {
        'name': 'comment',
        'label': '备注（其他信息、目标价等）',
        'columnWidth': 34.0,
      },
    ];
    // 标题
    List titleList = [];
    for (var i = 0; i < configure.length; i++) {
      var title = configure[i];
      sheet.getRangeByIndex(8, i + 1).columnWidth = title["columnWidth"];
      sheet.getRangeByIndex(8, i + 1).rowHeight = 20;
      titleList.add(title["label"]);
    }
    sheet.importList(titleList, 8, 1, false);
    for (var i = 0; i < datatable!.length; i++) {
      var text = datatable[i].toMap();
      // 正文
      var textList = [];
      for (var j = 0; j < configure.length; j++) {
        var title = configure[j];
        sheet.getRangeByIndex(i + 9, j + 1).columnWidth = title["columnWidth"];
        sheet.getRangeByIndex(i + 9, j + 1).rowHeight = 20;
        if (j == 0) {
          textList.add((i + 1).toString());
        } else {
          textList.add(text[title["name"]]);
        }
      }
      // 添加正文
      sheet.importList(textList, 9 + i, 1, false);
    }
    final Style titleStyle = workbook.styles.add('titleStyle');
    titleStyle.fontName = 'Times New Roman';
    titleStyle.fontSize = 22;
    // titleStyle.backColorRgb = Color.fromARGB(128, 242, 242, 242);
    titleStyle.bold = true;
    titleStyle.wrapText = false;
    titleStyle.hAlign = HAlignType.center;
    titleStyle.vAlign = VAlignType.center;
    sheet.getRangeByName('A1:A1').cellStyle = titleStyle;
    final Style headCommon = workbook.styles.add('headCommon');
    headCommon.fontName = 'Times New Roman';
    headCommon.fontSize = 12;
    // headCommon.backColorRgb = Color.fromARGB(128, 242, 242, 242);
    headCommon.bold = true;
    headCommon.wrapText = false;
    headCommon.hAlign = HAlignType.center;
    headCommon.vAlign = VAlignType.center;
    List headCommonList = [
      'C3:C3',
      'C4:C4',
      'F3:F3',
      'F4:F4',
      'F5:F5',
      'F6:F6',
      'A8:A8',
      'B8:B8',
      'E8:E8',
      'F8:F8',
      'G8:G8'
    ];
    for (var c = 0; c < headCommonList.length; c++) {
      sheet.getRangeByName(headCommonList[c]).cellStyle = headCommon;
    }
    // final Style yellowStyle = workbook.styles.add('yellowStyle');
    // yellowStyle.fontName = 'Times New Roman';
    // yellowStyle.fontSize = 12;
    // yellowStyle.backColorRgb = Color.fromARGB(128, 255, 255, 0);

    // yellowStyle.bold = true;
    // yellowStyle.wrapText = false;
    // yellowStyle.hAlign = HAlignType.center;
    // yellowStyle.vAlign = VAlignType.center;
    List yellowStyleList = [
      'A3:A3',
      'A5:A5',
      'A6:A6',
      'C5:C5',
      'C6:C6',
      'C8:C8',
      'D8:D8'
    ];
    for (var y = 0; y < yellowStyleList.length; y++) {
      sheet.getRangeByName(yellowStyleList[y]).cellStyle = headCommon;
    }
    final Style globalStyle = workbook.styles.add('globalStyle');
    globalStyle.fontName = 'Times New Roman';
    globalStyle.fontSize = 12;
    globalStyle.wrapText = false;
    globalStyle.hAlign = HAlignType.center;
    globalStyle.vAlign = VAlignType.center;
    List commonList = [
      'B3:B3',
      'B5:B5',
      'B6:B6',
      'D3:D3',
      'D4:D4',
      'D5:D5',
      'D6:D6',
      'G3:G3',
      'G4:G4',
      'G5:G5',
      'G6:G6'
    ];
    // var char = intToASCII(96 + configure.length);
    // sheet
    //     .getRangeByName('A9:' + 'G' + (datatable.length + 10).toString())
    //     .cellStyle = globalStyle;
    for (var n = 0; n < commonList.length; n++) {
      sheet.getRangeByName(commonList[n]).cellStyle = globalStyle;
    }
    for (var i = 0; i < datatable.length; i++) {
      sheet
          .getRangeByName('A' + (i + 9).toString() + ':A' + (i + 9).toString())
          .cellStyle = globalStyle;
    }
    for (var i = 0; i < datatable.length; i++) {
      sheet
          .getRangeByName('C' + (i + 9).toString() + ':C' + (i + 9).toString())
          .cellStyle = globalStyle;
    }
    for (var i = 0; i < datatable.length; i++) {
      sheet
          .getRangeByName('D' + (i + 9).toString() + ':D' + (i + 9).toString())
          .cellStyle = globalStyle;
    }
    for (var i = 0; i < datatable.length; i++) {
      sheet
          .getRangeByName('G' + (i + 9).toString() + ':G' + (i + 9).toString())
          .cellStyle = globalStyle;
    }
    // final Style blackStyle = workbook.styles.add('blackStyle');
    // blackStyle.fontName = 'Times New Roman';
    // blackStyle.fontSize = 12;
    // blackStyle.wrapText = false;
    // blackStyle.backColorRgb = Color.fromARGB(192, 192, 192, 192);
    // blackStyle.hAlign = HAlignType.center;
    // blackStyle.vAlign = VAlignType.center;
    for (var i = 0; i < datatable.length; i++) {
      sheet
          .getRangeByName('B' + (i + 9).toString() + ':B' + (i + 9).toString())
          .cellStyle = globalStyle;
    }
    for (var i = 0; i < datatable.length; i++) {
      sheet
          .getRangeByName('F' + (i + 9).toString() + ':F' + (i + 9).toString())
          .cellStyle = globalStyle;
    }
    sheet.getRangeByName('A7:G7').cellStyle = globalStyle;
    // final Style redStyle = workbook.styles.add('redStyle');
    // redStyle.fontName = 'Times New Roman';
    // redStyle.fontSize = 12;
    // redStyle.fontColor = '#FF0000';
    // redStyle.wrapText = false;
    // redStyle.hAlign = HAlignType.center;
    // redStyle.vAlign = VAlignType.center;
    for (var i = 0; i < datatable.length; i++) {
      sheet
          .getRangeByName('E' + (i + 9).toString() + ':E' + (i + 9).toString())
          .cellStyle = globalStyle;
    }
    // Save and dispose workbook.
    final List<int>? bytes = workbook.saveAsStream();
    workbook.dispose();
    // Download the output file in web.
    await FileSaveHelper.saveAndLaunchFile(bytes!, '$fileName.xlsx');
  }

  static dynamic intToASCII(int figure) {
    var listInt = [figure];
    var int2utf8 = Uint8List.fromList(listInt);
    return Utf8Codec().decode(int2utf8);
  }
}
